package com.leruan.aasimp.action.maintenance;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.entity.CarInsuranceCom;
import com.leruan.aasimp.entity.CarOdd;
import com.leruan.aasimp.entity.Eauser;
import com.leruan.aasimp.service.EauserService;
import com.leruan.aasimp.service.MaintenanceService;
import com.opensymphony.xwork2.ActionContext;

public class ApproveAction {


	private static final long serialVersionUID = -1906715812583393960L;
	
	private MaintenanceService maintenanceService;
	private EauserService eauserService; 
	
	public String showApprove(){
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Eauser eauser = (Eauser) request.getSession().getAttribute("eauser");
		
		List<CarOdd> list = maintenanceService.getOddByCompanyNm(eauser.getIdCompany()+"");
		for(CarOdd odd : list){
			System.out.println(odd.getPersonname());
			Eauser un = eauserService.getEauserByUn(odd.getPersonname());
			odd.setPersonname(un.getName());
		}
		JSONArray array = JSONArray.fromObject(list);
		System.out.println(array.toString()); 
		request.setAttribute("odds", array.toString().replace("\"", "'"));
		return "approve";
	}
	
	public String showodd(){
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
				.get(ServletActionContext.HTTP_REQUEST);
		Long oddId = Long.parseLong(request.getParameter("oddId"));
		CarOdd odd = maintenanceService.getOddByOid(oddId);
		Eauser un = eauserService.getEauserByUn(odd.getPersonname());
		odd.setPersonname(un.getName());
		request.setAttribute("oneOdd", odd);
		return "approve";
	}
	
	public String changeodd(){
		ActionContext context = ServletActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) context
		.get(ServletActionContext.HTTP_REQUEST);
		Long oddId = Long.parseLong(request.getParameter("oddId"));
		String status = request.getParameter("status");
		CarOdd odd = maintenanceService.getOddByOid(oddId);
		odd.setStatus(status);
		maintenanceService.updateOdd(odd);
		return showApprove();
	}

	public MaintenanceService getMaintenanceService() {
		return maintenanceService;
	}

	public void setMaintenanceService(MaintenanceService maintenanceService) {
		this.maintenanceService = maintenanceService;
	}

	public EauserService getEauserService() {
		return eauserService;
	}

	public void setEauserService(EauserService eauserService) {
		this.eauserService = eauserService;
	}

}
